package canhcuadaihoc.tracnghiemonline;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SimpleAdapter.ViewBinder;

public class LoginActivity extends Activity {
	public final int LOGIN_DIALOG_FAILED=1;
	public final int LOGIN_USER_WRONG=2;
	public final int LOGIN_PASS_WRONG=3;
	public final int LOGIN_PASS_USER_WRONG=4;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_user);
		
		final EditText edit_user=(EditText)findViewById(R.id.edit_username);
		final EditText edit_pass=(EditText)findViewById(R.id.edit_password);
		//Quản lý sự kiện Button Đăng Nhập
		Button bt_sign_in=(Button)findViewById(R.id.bt_sign_in);
		bt_sign_in.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(LoginUser(edit_user.getText().toString(), edit_pass.getText().toString())==true)
				{
					Toast.makeText(getApplicationContext(), "true", Toast.LENGTH_SHORT).show();
					Intent iNews= new Intent(getApplicationContext(), ContestActivity.class);
					startActivity(iNews);
				}

				else if(edit_user.getText().toString().equals("admin")==false)
					{						
						LoginActivity.this.showDialog(2);
					}
				else if(edit_pass.getText().toString().equals("admin")==false)
					{
						LoginActivity.this.showDialog(3);
					}
				else if(LoginUser(edit_user.getText().toString(), edit_pass.getText().toString())==false)
					{
						LoginActivity.this.showDialog(4);
					}
					
				}
			
		});
	
		//Quản Lý sự kiện Button Đăng Ký
		
	}

	/*
	 * Khởi tạo Dialog
	 * 
	 * */
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		Dialog dialog=null;
		//Login_Dialog.Builder _loginDialogBuilder=new Login_Dialog.Builder(LoginActivity.this);
		AlertDialog.Builder _alertDialogBuilder=new AlertDialog.Builder(LoginActivity.this);
		switch (id) {
		case 1:
			_alertDialogBuilder.setIcon(R.drawable.logo_small).setTitle("Login Error!").setMessage("Đăng Nhập Thất Bại").setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					LoginActivity.this.dismissDialog(LOGIN_DIALOG_FAILED);
				}
			});
			break;
		case 2:
			_alertDialogBuilder.setIcon(R.drawable.logo_small).setTitle("Login Error!").setMessage("Sai Username").setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					LoginActivity.this.dismissDialog(LOGIN_USER_WRONG);
				}
			});
			break;
		case 3:
			_alertDialogBuilder.setIcon(R.drawable.logo_small).setTitle("Login Error!").setMessage("Sai Password").setNegativeButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					LoginActivity.this.dismissDialog(LOGIN_PASS_WRONG);
				}
			});
			break;
		case 4:
			_alertDialogBuilder.setIcon(R.drawable.logo_small).setTitle("Login Error!").setMessage("Sai Username và Password").setNegativeButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					LoginActivity.this.dismissDialog(LOGIN_PASS_WRONG);
				}
			});
			break;
		case 5:
			_alertDialogBuilder.setIcon(R.drawable.logo_small).setTitle("Login Error!").setMessage("Sai Username và Password").setNegativeButton("OK", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					LoginActivity.this.dismissDialog(5);
				}
			});
			break;
		default:
			break;
		}
		dialog=_alertDialogBuilder.create();
		//dialog=_loginDialogBuilder.create();
		return dialog;
	}
	/*
	 * 
	 * Đăng Nhập tài khoản người dùng
	 * 
	 * */
	public boolean LoginUser(String User,String Password)
	{
		if(User.equals("admin")==true && Password.equals("admin")==true)
			return true;
		return false;
	}
}
